# List of the object files of the present folder
OBJ_TEST= mod_bubble_test.o \
     mod_smithhutton_test.o \
     mod_zunino2009_test.o  \
     mod_sbrot_test.o \
     mod_darcy_test.o \
     mod_periodic_test.o \
     mod_fracture_test.o
OBJ= mod_error_norms.o \
     mod_testcases.o \
     $(OBJ_TEST)
OBJ_CG= cg.o \
     mod_cg_locmat.o \
     $(OBJ)
OBJ_LDGH= ldgh.o \
     mod_ldgh_locmat.o \
     mod_ldgh_reconstructions.o \
     mod_tau.o \
     mod_ldgh_linpb.o \
     $(OBJ)
OBJ_LDGHF= ldghf.o \
     mod_ldgh_locmat.o \
     mod_ldg_locmat.o \
     mod_tau.o \
     $(OBJ)
OBJ_PSI= psi.o \
     mod_psi_locmat.o \
     $(OBJ)
OBJ_SG= mod_scharfetter_gummel.o \
     mod_sc_locmat.o
# These are the modules that are meant to be USEd by the library users
LIBRARY_MOD_FILES= mod_scharfetter_gummel.mod \
     mod_sc_locmat.mod

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(BUILDDIR)/ADR

LIBS:=-lfml_general_utilities -lfml_linear_solvers \
  -lfem $(LIBS)
LDFLAGS:=$(LDFLAGS) -L$(LIBDIR)

# VPATH includes:
#   PWD for the source files (included by default)
#   PWD/testcases for the source files of the testcases
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the libfml_general_utilities.a library
#   BINDIR for the final executable
VPATH:=./testcases $(BUILDDIR) $(LIBDIR) $(BINDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# Main targets ---------------------------------------

cg: $(OBJ_CG)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_CG) $(LIBS) -o $(BINDIR)/cg

ldgh: $(OBJ_LDGH)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_LDGH) $(LIBS) -o $(BINDIR)/ldgh

ldghf: $(OBJ_LDGHF)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_LDGHF) $(LIBS) -o $(BINDIR)/ldghf

psi: $(OBJ_PSI)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_PSI) $(LIBS) -o $(BINDIR)/psi

scharfetter_gummel: libsc.a

libsc.a: $(OBJ_SG)
	$(RM) $(LIBDIR)/$@
	cd $(BUILDDIR) && $(AR) -r $(LIBDIR)/$@ $(OBJ_SG)
	for mod in $(LIBRARY_MOD_FILES);     \
        do                                   \
          cp $(BUILDDIR)/$${mod} $(LIBDIR)/; \
        done

# General rule: all the .o depend on the various libraries
%.o: %.f90 libfml_general_utilities.a libfem.a
	cd $(BUILDDIR) && \
	  $(FC) $(FFLAGS) $(MODFLAG)$(LIBDIR) -c $(PWD)/$<

# Some .o also depend on the libfml_linear_solvers library
mod_ldgh_linpb.o ldgh.o: libfml_linear_solvers.a

clean:
	$(RM) $(BUILDDIR)/*.o $(BUILDDIR)/*.mod \
	  $(BINDIR)/cg $(BINDIR)/ldgh $(BINDIR)/ldghf $(BINDIR)/psi \
	  $(LIBDIR)/libsc.a
	for mod in $(LIBRARY_MOD_FILES); \
        do                               \
          $(RM) $(LIBDIR)/$${mod};       \
        done

# Specific dependencies: because of USE directives ---
# Notice: here we list only the prerequisites which are not already
# included in the libfml_general_utilities.a library, which is a
# prerequisite of all the .o files listed in this Makefile

mod_testcases.o: \
   $(OBJ_TEST)

cg.o: \
   mod_testcases.o \
   mod_cg_locmat.o \
   mod_error_norms.o

mod_cg_locmat.o: \
   mod_testcases.o

ldgh.o: \
   mod_testcases.o \
   mod_tau.o \
   mod_ldgh_locmat.o \
   mod_ldgh_reconstructions.o \
   mod_ldgh_linpb.o \
   mod_error_norms.o

ldghf.o: \
   mod_testcases.o \
   mod_tau.o \
   mod_ldgh_locmat.o \
   mod_ldg_locmat.o

mod_ldgh_locmat.o: \
   mod_tau.o \
   mod_testcases.o

mod_ldg_locmat.o: \
   mod_testcases.o

mod_ldgh_reconstructions.o: \
   mod_tau.o \
   mod_ldgh_locmat.o \
   mod_testcases.o

mod_tau.o: \
   mod_testcases.o

mod_ldgh_linpb.o: \

psi.o: \
   mod_testcases.o \
   mod_psi_locmat.o \
   mod_error_norms.o

mod_psi_locmat.o: \
   mod_testcases.o

mod_sc_locmat.o: \
   mod_scharfetter_gummel.o
